package cn.com.huzo.webapp.controller.trade;

import cn.com.huzo.model.entities.trade.DeliveryCommandSheetHeaderEntity;
import cn.com.huzo.model.entities.trade.DeliveryCommandSheetLinesEntity;
import cn.com.huzo.model.entities.trade.DeliveryCommandSheetLinesMoEntity;
import huzofw.common.web.controller.BaseFormController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import cn.com.huzo.service.trade.DeliveryCommandSheetHeaderManager;
import cn.com.huzo.service.trade.DeliveryCommandSheetLinesManager;
import cn.com.huzo.service.trade.DeliveryCommandSheetLinesMoManager;
import java.util.ArrayList;
import org.apache.commons.collections4.MapUtils;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * 发货发令单画面控制器
 *
 * @author 吴旭
 */
@Controller
@RequestMapping(value = "/trade/delivery_command_sheet")
public class DeliveryCommandSheetController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    @Resource(name = "DeliveryCommandSheetHeaderManager")
    private DeliveryCommandSheetHeaderManager _manager_header;

    @Resource(name = "DeliveryCommandSheetLinesMoManager")
    private DeliveryCommandSheetLinesMoManager _manager_lines_mo;

    @Resource(name = "DeliveryCommandSheetLinesManager")
    private DeliveryCommandSheetLinesManager _manager_lines;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到发货发令单一览画面
     * <p>
     * @param request
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("/list.html")
    public ModelAndView redirectListWnd(HttpServletRequest request) throws Exception {
        ModelAndView view = new ModelAndView();
        view.setViewName("trade.delivery_command_sheet.list.def");
        return view;
    }

    /**
     * 跳转发货发令单编辑画面
     * <p>
     * @param request 上下文请求
     * @param action 操作标志 1;增加 2：编辑
     * @param headerId 发货发令单标识
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("/{action}/{headerId}/edit.html")
    public ModelAndView redirectEditWnd(HttpServletRequest request, @PathVariable(value = "action") int action,
            @PathVariable(value = "headerId") Long headerId) throws Exception {
        ModelAndView view = new ModelAndView();
        view.addObject("action", action);
        view.addObject("headerId", headerId.toString());
        view.setViewName("trade.delivery_command_sheet.edit.def");
        return view;
    }

    /**
     * 款号显示顺序
     *
     * @return
     */
    @RequestMapping("/lines/styleno_order/edit.html")
    public ModelAndView redirectLinesEditStyleNoOrderPopupWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.trade.delivery_command_sheet_lines_styleno_order.edit.def");
        return view;
    }

    /**
     * 弹出发货发令单选择画面
     * <p>
     * @return
     */
    @RequestMapping("/choice.html")
    public ModelAndView redirectHeaderPopupWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.trade.delivery_command_sheet.choice.def");
        return view;
    }

    /**
     * 弹出发货发令单-明细选择画面
     * <p>
     * @return
     */
    @RequestMapping("/lines/choice.html")
    public ModelAndView redirectLinesPopupWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.trade.delivery_command_sheet_lines.choice.def");
        return view;
    }

    /**
     * 发货发令单-打印预览画面
     * <p>
     * @return
     */
    @RequestMapping("/print.html")
    public ModelAndView redirectPrintPreviewWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("print.trade.delivery_command_sheet.print.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="主表处理部分">
    /**
     * 得到新的单据号
     * <p>
     * @return @throws Exception
     */
    @RequestMapping(value = "/header/fetchNewBillNo.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchNewNoOfBill() throws Exception {
        Map<String, Object> rtnVal = new HashMap();
        rtnVal.put("BillNo", _manager_header.fetchNewBillNo());
        return rtnVal;
    }

    /**
     * 根据ID取得发货发令单数据。
     *
     * @param criterion 发货发令单标识、当前用户、流程标识
     * @return 发货发令单数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/header/fetchByKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchByKey(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_header.fetchByHeaderKey(MapUtils.getLong(criterion, "HeaderId"));
    }

    /**
     * 根据标识取得款号列表
     *
     * @param headerKey 主表标识
     * @return 款明细
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/header/fetchStyleNoByHeaderKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    String[] fetchStyleNoByHeaderKey(@RequestBody Long headerKey) throws Exception {
        String strStyleNo = _manager_header.fetchStyleNoByHeaderKey(headerKey);

        return strStyleNo.split(",");
    }

    /**
     * 根据条件取得发货发令单信息分页数据。
     * <p>
     * @param criterion 检索条件
     * @return 分页数据
     * @throws Exception
     */
    @RequestMapping(value = "/header/fetchForPage.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchForPage(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager_header.fetchForPage(criterion);
    }

    /**
     * 保存发货发令单信息数据。
     * <p>
     * @param instance 要保存的数据
     * @return 新增记录主键.
     * @throws Exception
     */
    @RequestMapping(value = "/header/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Long save(@RequestBody DeliveryCommandSheetHeaderEntity instance) throws Exception {
        Long rowsAffected;

        if (instance.getHeaderId() > 0) { //编辑
            rowsAffected = (long) _manager_header.update(instance);
        } else {//新增
            rowsAffected = _manager_header.insert(instance);
        }

        return rowsAffected;
    }

    /**
     * 删除发货发令单信息。
     *
     * @param criterion 要删除的数据
     * @return 返回删除后的数据
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/header/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> delete(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_header.delete(criterion);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="引入指示书处理部分">
    /**
     * 引入指示书明细同时增加发货发令单明细数据。
     * <p>
     * @param itemList 要保存的分配明细。
     * @return 影响的行数（引入的指示书明细记录）。
     * @throws Exception
     */
    @RequestMapping(value = "/lines_mo/insert.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> importMo(@RequestBody List<DeliveryCommandSheetLinesMoEntity> itemList) throws Exception {
        return _manager_lines_mo.batchInsert(itemList);
    }

    /**
     * 引入指示书明细同时增加发货发令单明细数据。
     * <p>
     * @param itemList 要保存的分配明细。
     * @return 影响的行数（引入的指示书明细记录）。
     * @throws Exception
     */
    @RequestMapping(value = "/lines_mo/update.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> updateLinesMo(@RequestBody List<DeliveryCommandSheetLinesMoEntity> itemList) throws Exception {
        if (itemList.isEmpty()) {
            return new HashMap();
        } else {
            return _manager_lines_mo.batchUpdate(itemList);
        }
    }

    /**
     * 删除引入的指示书。
     *
     * @param instance
     * @return 影响行数
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/lines_mo/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> deleteLinesMo(@RequestBody DeliveryCommandSheetLinesMoEntity instance) throws Exception {
        return _manager_lines_mo.deleteLines(instance);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="明细处理部分">
    /**
     * 根据主记录ID和款号得到款号对应的全部明细数据列表 （用于弹出调用）
     *
     * @param criterion 条件
     * @return 全部明细数据列表
     * @throws java.lang.Exception
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/lines/fetchByHeaderIdAndStyleNo.html",
            method = RequestMethod.POST,
            produces = {"application/xml", "application/json"})
    public @ResponseBody
    List<DeliveryCommandSheetLinesEntity> fetchByHeaderIdAndStyleNo(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines.fetchByHeaderIdAndStyleNo(
                MapUtils.getLong(criterion, "HeaderId"),
                MapUtils.getString(criterion, "StyleNo"));
    }

    /**
     * 更新发货发令单分配明细信息数据。
     * <p>
     * @param itemList 要更新的数据。
     * @return 影响的行数。
     * @throws Exception
     */
    @RequestMapping(value = "/lines/update.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> saveLines(@RequestBody List<DeliveryCommandSheetLinesEntity> itemList) throws Exception {
        if (itemList.isEmpty()) {
            return new HashMap();
        } else {
            return _manager_lines.batchUpdate(itemList);
        }
    }

    /**
     * 删除发货发令单明细信息。
     *
     * @param criterion
     * @return 影响行数
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/lines/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> deleteLines(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines.delete(criterion);
    }

    /**
     * 批量删除发货发令单明细信息。
     *
     * @param criterion
     * @return 影响行数
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/lines/batch_delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> batchDeleteLines(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines.batchDelete(criterion);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="款号顺序处理部分">
    /**
     * 取得明细中款号顺序数据
     * <p>
     * @param headerId 生产指示书标识
     * @return 款号数据
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/lines/fetchStyleNoOrder.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<Map<String, Object>> fetchStyleNoOrder(@RequestBody final Long headerId) throws Exception {
        List<Map<String, Object>> _result = _manager_lines.fetchLinesStyleNoOrderByHeaderId(headerId);
        return _result == null ? new ArrayList() : _result;
    }

    /**
     * 设置款号顺序。
     *
     * @param itemList 要更新的款号顺序
     * @return 更新后的发货发令单明细
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/lines/updateStyleNoOrder.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> updateLinesStyleNoOrder(@RequestBody final List<Map<String, Object>> itemList) throws Exception {
        return _manager_lines.updateLinesStyleNoOrder(itemList);
    }
    // </editor-fold>
}
